﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using HonStats.Heartbeat.Model;
using HonStats.Heartbeat.Library;

namespace HonStats.Heartbeat
{
    public class Parser
    {

        
        
        private static Hashtable DeserializePHPData(string phpString)
        {
            var serializer = new PHPSerializer();
            Hashtable ht = (Hashtable)serializer.Deserialize(phpString);//Deserialize the Hashtable

            return ht;
        }

        public static List<User> Parse(string userData)
        {

            List<User> user = new List<User>();
            
            Hashtable results = Parser.DeserializePHPData(userData);
            try // reason i added a a try-catch is because it throws errors sometimes on empty data this keeps it going.
            {
                foreach (DictionaryEntry Item in (Hashtable)((Hashtable)results["all_stats"])) //adds a user to the list for multiple user request
                {
                    Hashtable allStats = (Hashtable)((Hashtable)results["all_stats"])[Item.Key];

                    user.Add(new User { account_id = Convert.ToInt32(allStats["account_id"]), nickname = allStats["nickname"].ToString() });
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Dead Account");
            }

            return user;
        }
    }
}
